package net.yambal.RTM.Utilty
{
	import net.yambal.RTM.Items.RTMContact;
	import net.yambal.RTM.Items.RTMGroup;
	import net.yambal.RTM.Items.RTMList;
	import net.yambal.RTM.Items.RTMLocation;
	import net.yambal.RTM.Items.RTMNote;
	import net.yambal.RTM.Items.RTMTask;
	import net.yambal.RTM.Items.RTMTaskSeries;
	import net.yambal.RTM.Items.RTMTimezone;
	import net.yambal.RTM.Items.RTMUser;
	
	public class Parser
	{
		public function Parser()
		{
		}

		/**
		 * noteのXMLListからArrayを返す
		 **/
		static public function NoteXMLListToNote(xl:XMLList):Array{
			var ret:Array = new Array();
			
			for(var n_num:int = 0; n_num < xl.length(); n_num++){
				var note:RTMNote = new RTMNote();
				note.created = RTMDateUtil.DateParce(xl[n_num].@created);
				note.modified = RTMDateUtil.DateParce(xl[n_num].@modified);
				note.id = xl[n_num].@id;
				note.title = xl[n_num].@title;
				note.note = xl[n_num];
				ret.push(note);
			}
			
			return ret;
		}
		
		/**
		 * timezoneのXMLListからArrayを返す
		 **/
		static public function TimeZoneXMLListToTimezone(xl:XMLList):Array{
			var ret:Array = new Array();
			
			for(var n_num:int = 0; n_num < xl.length(); n_num++){
				var tz:RTMTimezone = new RTMTimezone();
				tz.id = RTMIntUtility.StringToInt(xl[n_num].@id);
				tz.dst = RTMIntUtility.StringToInt(xl[n_num].@dst);
				tz.current_offset = Number(xl[n_num].@current_offset);
				tz.offset = Number(xl[n_num].@offset);
				tz.name = xl[n_num].@name;
				ret.push(tz);
			}
			
			return ret;
		}
		
		// ---------------------------------------------------------------------------
		/**
		 * taskseriesのXMLListからArrayを返す
		 ***/
		static public function TaskSeriesXMLListToTaskSeries(xl:XMLList):Array{
			var ret:Array = new Array();
			for(var n_num:int = 0; n_num < xl.length(); n_num++){
				ret.push(TaskSeriesXMLToTaskSeries(xl[n_num]));
			}
			return ret;
		}
		
		// taskseriesのXMLからRTMTaskSeriesを返す
		static public function TaskSeriesXMLToTaskSeries(x:XML):RTMTaskSeries{
			var ts:RTMTaskSeries = new RTMTaskSeries();

			ts.id = x.@id;
			ts.list_id = x.parent().@id;
			ts.created = RTMDateUtil.DateParce(x.@created);
			ts.modified = RTMDateUtil.DateParce(x.@modified);
			ts.name = x.@name;
			ts.source = x.@source;
			ts.url = x.@url;
			
			//participants
			ts.participants = new Array();
			var part:XMLList = x.descendants("contact");
			for(var part_num:int = 0; part_num < part.length(); part_num++){
				var part_user:RTMUser = new RTMUser();
				part_user.id = part[part_num].@id;
				part_user.user_name = part[part_num].@username;
				part_user.full_name = part[part_num].@fullname;
				ts.participants.push(part_user);
			}
			
			// note
			ts.notes = Parser.NoteXMLListToNote(x.descendants("note"));
			
			// Tag
			ts.tags = new Array();
			var tags:XMLList = x.descendants("tag");
			for(var t_num:int = 0; t_num < tags.length(); t_num++){
				ts.tags.push(tags[t_num]);
			}
			
			//Task
			ts.task = new RTMTask();
			ts.task.id = x.task.@id;
			ts.task.added = RTMDateUtil.DateParce(x.task.@added);
			ts.task.completed = RTMDateUtil.DateParce(x.task.@completed);
			ts.task.due = RTMDateUtil.DateParce(x.task.@due)
			ts.task.deleted = RTMBooleanUtil.toBoolean(x.task.@deleted);
			ts.task.estimate = RTMIntUtility.StringToInt(x.task.@estimate);
			ts.task.has_due_time = RTMBooleanUtil.toBoolean(x.task.@has_due_time);
			ts.task.postponed = int(x.task.@postponed);
			ts.task.priority = x.task.@priority;
			
			return ts;
		}
		
		// ---------------------------------------------------------------------------
		/**
		 * 
		 **/
		static public function LocationXMLListToLocation(xl:XMLList):Array{
			var ret:Array = new Array();
			for(var n_num:int = 0; n_num < xl.length(); n_num++){
				var l:RTMLocation = new RTMLocation();
				l.id = xl[n_num].@id;
				l.address = xl[n_num].@address;
				l.latitude = xl[n_num].@latitude;
				l.longitude = xl[n_num].@longitude;
				l.name = xl[n_num].@name;
				l.viewable = RTMBooleanUtil.toBoolean(xl[n_num].@viewable);
				l.zoom = xl[n_num].@zoom;
				ret.push(l);
			}
			return ret;
		}
		
		// ---------------------------------------------------------------------------
		
		static public function GroupsXMLListToGroup(xl:XMLList):Array{
			var ret:Array = new Array();
			for(var n_num:int = 0; n_num < xl.length(); n_num++){
				var g:RTMGroup = GroupsXMLToGroup(xl[n_num]);
				ret.push(g);
			}
			return ret;
		}
		
		static public function GroupsXMLToGroup(xl:XML):RTMGroup{
			var g:RTMGroup = new RTMGroup();
			g.id = xl.@id;
			g.name = xl.@name;
			g.contacts = ContactXMMLListToContact(xl.descendants("contact"));
			return g;
		}
		
		// ---------------------------------------------------------------------------
		static public function ContactXMMLListToContact(xl:XMLList):Array{
			var ret:Array = new Array();
			for(var n_num:int = 0; n_num < xl.length(); n_num++){
				ret.push(ContactXMMLToContact(xl[n_num]));
			}
			return ret;
		}
		
		static public function ContactXMMLToContact(xl:XML):RTMContact{
			var ret:RTMContact = new RTMContact();
			ret.id = xl.@id;
			ret.username = xl.@username;
			ret.fullname = xl.@fullname;
			return ret;
		}
		
		// ---------------------------------------------------------------------------
		static public function ListXMLListToList(x:XMLList):Array{
			var ret:Array = new Array();
			for(var l_num:int = 0; l_num < x.length(); l_num++){
				ret.push(ListXMLToList(x[l_num]));
			}
			return ret;
		}
		
		
		/**
		 * <list...のXMLからRTMListを返す
		 **/
		static public function ListXMLToList(x:XML):RTMList{
			var ret:RTMList = new RTMList();
			ret.archived = RTMBooleanUtil.toBoolean(x.@archived);
			ret.deleted = RTMBooleanUtil.toBoolean(x.@deleted);
			ret.id = x.@id;
			ret.locked = RTMBooleanUtil.toBoolean(x.@locked);
			ret.name = x.@name;
			ret.position = x.@position;
			ret.smart = RTMBooleanUtil.toBoolean(x.@smart);
			ret.sort_order = x.@sort_order;
			
			return ret;
		}
	}
}